﻿using System;
using System.Data.Common;
using System.Data.SQLite;

namespace DbUpgrader.Apis.Db.Sqlite
{
    public class SqliteDb : IDb
    {
        private readonly SQLiteConnection cnn;

        public SqliteDb(SQLiteConnection cnn)
        {
            this.cnn = cnn;
        }

        public void ExecuteRequest(string request)
        {
            using (DbCommand cmd = cnn.CreateCommand())
            {
                cmd.CommandText = request;
                cmd.ExecuteNonQuery();
            }
        }

        public T ExecuteQuery<T>(string request)
        {
            SQLiteCommand command = cnn.CreateCommand();
            command.CommandText = request;
            return (T) Convert.ChangeType(command.ExecuteScalar(), typeof (T), null);
        }
    }
}